package cn.gok.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @Author: chenfan
 * @Date: 2020/12/6 15:26
 * @Description: 自定义响应对象
 */
public class MyResponse {
    private OutputStream outputStream;

    public MyResponse() {
    }

    public MyResponse(OutputStream outputStream) {
        this.outputStream = outputStream;
    }


    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }

    /**
     * 先根据输入的url来获取到静态资源的绝对路径 ，然后再根据绝对路径将得到的数据输出
     * @param path
     */
    public void outputHtml(String path) throws IOException {
//        String abslouteResourcePath = StaticResourceUtil.getAbsolutePath(path);

        File file = new File(path);
        if (file.exists()){
            StaticResourceUtil.outputStaticReource(new FileInputStream(file),outputStream);
        }else{
            output(HttpProtocolUtil.getHttpHeader404());
        }
    }
}
